package com.shop.cloud.upms.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.aliyuncs.exceptions.ClientException;
import com.shop.cloud.common.core.constant.SecurityConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.common.sms.config.SmsConfigProperties;
import com.shop.cloud.common.sms.util.SmsUtils;
import com.shop.cloud.upms.common.entity.SysConfigSms;
import com.shop.cloud.upms.common.entity.SysSms;
import com.shop.cloud.upms.common.feign.FeignConfigSmsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author 
 * @date 2019/07/14
 * 短信管理
 */
@RestController
@AllArgsConstructor
@RequestMapping("/sms")
@Tag(description = "sms", name = "短信管理")
public class SmsController {
	private final FeignConfigSmsService feignConfigSmsService;

	/**
	 * 发送短信
	 * @param sysSms
	 * @return
	 */
	@Operation(summary = "发送短信")
	@SysLog("发送短信")
	@PostMapping("/send")
	@PreAuthorize("@ato.hasAuthority('sys:sms:add')")
	public R sendEmail(@RequestBody SysSms sysSms) throws ClientException {
		SysConfigSms sysConfigSms = feignConfigSmsService.getSysConfigSms(SecurityConstants.FROM_IN).getData();
		if(sysConfigSms == null){
			return R.failed("未配置短信账号");
		}
		SmsConfigProperties smsConfigProperties = new SmsConfigProperties();
		BeanUtil.copyProperties(sysConfigSms, smsConfigProperties);
		SmsUtils.sendSms(smsConfigProperties, sysSms.getSignName(), sysSms.getPhoneNumbers(), sysSms.getTemplateCode(), sysSms.getTemplateParam());
		return R.ok();
	}
}
